from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


class OrdersSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(label='创建日期',
                                            format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = OrderInfo
        fields = ('order_id', 'create_time')


class SKUSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ('id', 'name', 'default_image')


class OrderGoodsSerializer(serializers.ModelSerializer):
    sku = SKUSerializer(label='sku')

    class Meta:
        model = OrderGoods
        fields = ('id', 'count', 'price', 'sku')


class OrderSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(label='用户')
    skus = OrderGoodsSerializer(label='skus', many=True)
    create_time = serializers.DateTimeField(label='下单时间',
                                            format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = OrderInfo
        exclude = ('address', 'update_time')


class StatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = ('order_id', 'status')
        extra_kwargs = {
            'order_id': {'read_only': True}
        }
